package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> pageNursing(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.pageNursing(name, status);
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public void addNursingData(NursingProjectDto nursingProjectDto) {
        nursingProjectMapper.insert(BeanUtil.toBean(nursingProjectDto, NursingProject.class));
    }

    @Override
    public NursingProjectVo getNursingProject(Long id) {

        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        nursingProjectMapper.update(BeanUtil.toBean(nursingProjectDto, NursingProject.class));
    }

    @Override
    public void deleteNursingProject(Long id) {
        Long count = nursingProjectMapper.getCount(id);
        if (count != null && count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE);
        }
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public void enableOrForbidden(Long id, Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        List<NursingProject> nursingProjectMapperAll = nursingProjectMapper.getAll();
        return nursingProjectMapperAll.stream().map(npm -> BeanUtil.toBean(npm, NursingProjectVo.class)).collect(Collectors.toList());
    }
}
